<?php
class CountriesController extends AppController {

	var $name = 'Countries';
        var $uses=array('Country','Continent','BlockCountry');

        function beforeFilter(){
		parent::beforeFilter();	  	
	  	$this->Auth->allowedActions = array('*');
		
	}

	function block_countries() {
            $this->layout='innerhome';
            $this->Country->recursive = -1;
            $this->BlockCountry->recursive = -1;
            if(!empty ($this->data)){
                if(!empty ($this->data['country_name'])){
                $totalcountry=$this->Country->find('count');
                for($i=0;$i<$totalcountry;$i++){

                if(isset ($this->data['country_name'][$i])){
                $str=explode('_', $this->data['country_name'][$i]);
                    $country=$str[0];
                    $con=$str[1];
                    $cunt=$this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>$con,'BlockCountry.country_id' =>$country)));
                    if($cunt<1){
                        $this->data['BlockCountry']['id']=null;
                        $this->data['BlockCountry']['seller_id']=$this->Auth->user('id');
                        $this->data['BlockCountry']['country_id']=$country;
                        $this->data['BlockCountry']['continent_id']=$con;
                       $this->BlockCountry->save($this->data);
                    }
                }else{

                    $str=explode('_', $this->data['con_name'][$i]);
                    $country=$str[0];
                    $con=$str[1];
                    $cunt=$this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>$con,'BlockCountry.country_id' =>$country)));
                    if($cunt>0){
                        $con=array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>$con,'BlockCountry.country_id' =>$country);
                       $this->BlockCountry->deleteAll($con);
                    }
                }
                                    
                }
                $this->Session->setFlash(__('The Block countries has been Updated Successfully.', true));

                }else{
                     $totalcountry=$this->Country->find('count');
                for($i=0;$i<$totalcountry;$i++){
                  $str=explode('_', $this->data['con_name'][$i]);
                    $country=$str[0];
                    $con=$str[1];
                    $cunt=$this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>$con,'BlockCountry.country_id' =>$country)));
                    if($cunt>0){
                        $con=array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>$con,'BlockCountry.country_id' =>$country);
                       $this->BlockCountry->deleteAll($con);
                    }
                 }
                 $this->Session->setFlash(__('The Block countries has been Updated Successfully.', true));
                }
                
            }
           
            $check = $this->BlockCountry->find('all', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id')),'fields'=>array('country_id')));
            $i=0;
            if(!empty ($check)){
            foreach ($check as $chkv):
                $cid[$i]=$chkv['BlockCountry']['country_id'];
            $i++;
            endforeach;
            }else{

                $cid=array();
            }
           $this->set('cid',$cid);
            $this->set('cna', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>1))));
            $this->set('na', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>1))));
            $this->set('csa', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>2))));
            $this->set('sa', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>2))));
            $this->set('cep', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>3))));
            $this->set('ep', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>3))));
            $this->set('cas', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>4))));
            $this->set('as', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>4))));
            $this->set('coc', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>5))));
            $this->set('oc', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>5))));
            $this->set('caf', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>6))));
            $this->set('af', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>6))));
	}

        function backstage_block_countries() {
            $this->layout='admin';
            $this->Country->recursive = -1;
            $this->BlockCountry->recursive = -1;
            if(!empty ($this->data)){
                if(!empty ($this->data['country_name'])){
                $totalcountry=$this->Country->find('count');
                for($i=0;$i<$totalcountry;$i++){

                if(isset ($this->data['country_name'][$i])){
                $str=explode('_', $this->data['country_name'][$i]);
                    $country=$str[0];
                    $con=$str[1];
                    $cunt=$this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>$con,'BlockCountry.country_id' =>$country)));
                    if($cunt<1){
                        $this->data['BlockCountry']['id']=null;
                        $this->data['BlockCountry']['seller_id']=$this->Auth->user('id');
                        $this->data['BlockCountry']['country_id']=$country;
                        $this->data['BlockCountry']['continent_id']=$con;
                       $this->BlockCountry->save($this->data);
                    }
                }else{

                    $str=explode('_', $this->data['con_name'][$i]);
                    $country=$str[0];
                    $con=$str[1];
                    $cunt=$this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>$con,'BlockCountry.country_id' =>$country)));
                    if($cunt>0){
                        $con=array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>$con,'BlockCountry.country_id' =>$country);
                       $this->BlockCountry->deleteAll($con);
                    }
                }
                                    
                }
                $this->Session->setFlash(__('The Block countries has been Updated Successfully.', true));

                }else{
                     $totalcountry=$this->Country->find('count');
                for($i=0;$i<$totalcountry;$i++){
                  $str=explode('_', $this->data['con_name'][$i]);
                    $country=$str[0];
                    $con=$str[1];
                    $cunt=$this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>$con,'BlockCountry.country_id' =>$country)));
                    if($cunt>0){
                        $con=array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>$con,'BlockCountry.country_id' =>$country);
                       $this->BlockCountry->deleteAll($con);
                    }
                 }
                 $this->Session->setFlash(__('The Block countries has been Updated Successfully.', true));
                }
                
            }
           
            $check = $this->BlockCountry->find('all', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id')),'fields'=>array('country_id')));
            $i=0;
            if(!empty ($check)){
            foreach ($check as $chkv):
                $cid[$i]=$chkv['BlockCountry']['country_id'];
            $i++;
            endforeach;
            }else{

                $cid=array();
            }
           $this->set('cid',$cid);
            $this->set('cna', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>1))));
            $this->set('na', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>1))));
            $this->set('csa', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>2))));
            $this->set('sa', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>2))));
            $this->set('cep', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>3))));
            $this->set('ep', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>3))));
            $this->set('cas', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>4))));
            $this->set('as', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>4))));
            $this->set('coc', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>5))));
            $this->set('oc', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>5))));
            $this->set('caf', $this->BlockCountry->find('count', array('conditions' => array('BlockCountry.seller_id' => $this->Auth->user('id'),'BlockCountry.continent_id' =>6))));
            $this->set('af', $this->Country->find('all',array('conditions'=>array('Country.continent_id'=>6))));
	}

	function backstage_view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid country', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('country', $this->Country->read(null, $id));
	}

	function backstage_add() {
		if (!empty($this->data)) {
			$this->Country->create();
			if ($this->Country->save($this->data)) {
				$this->Session->setFlash(__('The country has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The country could not be saved. Please, try again.', true));
			}
		}
		$continents = $this->Country->Continent->find('list');
		$this->set(compact('continents'));
	}

	function backstage_edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid country', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Country->save($this->data)) {
				$this->Session->setFlash(__('The country has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The country could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Country->read(null, $id);
		}
		$continents = $this->Country->Continent->find('list');
		$this->set(compact('continents'));
	}

	function backstage_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for country', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Country->delete($id)) {
			$this->Session->setFlash(__('Country deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Country was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>